//
// $.ui.dialog.maxZ - максимальный z-index
//
function app_class() {
    this.paginator = { current:0, total:0 };
	this.customer = { full_name:'', cat_id:0 };
	this.ui_dialog_min_z_index = 100;
	this.jqgrid_form_min_z_index = 8000;
	this.alert_min_z_index = 9000;
};

app_class.prototype.alert = function (dlgID, title, msg, icon, doing, param) {
    var aldlg = $('#' + dlgID);
    var newdlg = $('#' + dlgID + '_mess').length ? false : true;

    if (newdlg)
        aldlg.html(
'<table id="' + dlgID + '_mess" style="display:none; border:thin solid transparent; height:100%; width:100%"><tr>' +
'    <td style="padding-left:64px; padding-right:10px; vertical-align:middle;"></td>' +
'</tr></table>'
);

    aldlg.dialog(
        {
            modal: true, title: title,
//            zIndex: app.alert_min_z_index,
            height: 180,
            width: 360,
            autoOpen: false,
            resizable: false,
            open: function (e,ui) {
//				$('div.ui-widget-overlay.ui-front').css({'z-index':9990});
//				$('div[aria-describedby="' + dlgID + '"]').css({'z-index':9991}).shadow();
				$('div[aria-describedby="' + dlgID + '"]').shadow(); 
			},
            close: function () {
                $(this).dialog("destroy");
//                $.ui.dialog.maxZ = $.maxZIndex({ group: '.ui-dialog.ui-widget' });
            },
            buttons: { "Закрыть": function () { 
                    $(this).dialog("close");
                    if (doing) doing(param);
                } 
            }
        }
    );
    aldlg.before('<div id="' + dlgID + '_icon" class="' + icon + '" ' +
        'style="display:none;position:absolute;left:16px;top:64px;"></div>');

    $('#' + dlgID + '_mess td').html(msg);
    aldlg.dialog('open');
    $('#' + dlgID + '_icon').show();
    $('#' + dlgID + '_mess').show();
}

app_class.prototype.confirm = function( dlgID, title, msg, icon, doing, param ) {
var aldlg = $('#'+dlgID);
var newdlg = $('#'+dlgID+'_mess').length ? false : true;
var caller = arguments.callee.caller;

if( newdlg )
    aldlg.html(
'<table id="'+dlgID+'_mess" style="display:none; border:thin solid transparent; height:100%; width:100%"><tr>'+
'    <td style="padding-left:64px; padding-right:10px; vertical-align:middle;"></td>'+
'</tr></table>'
);

    aldlg.dialog( 
        {
            modal: true, title: title,
//            zIndex: app.alert_min_z_index,
            height: 180, width: 360,
            autoOpen: false, resizable: false,
            open: function () { 
				$('div[aria-describedby="' + dlgID + '"]').shadow(); 
			},
            close: function () {
                $(this).dialog("destroy");
//                $.ui.dialog.maxZ = $.maxZIndex({ group: '.ui-dialog.ui-widget' });
            },
            buttons: {
                "Да": function () {
                        $(this).dialog("close");
// Выполним callback в контексте вызывателя
                        doing.call(caller,param); 
                    },
                "Нет": function () {
                        $(this).dialog("close");
                    }
            }
        }
    );
    aldlg.before( '<div id="'+dlgID+'_icon" class="'+icon+'" '+
        'style="display:none;position:absolute;left:16px;top:64px;"></div>' );    

    $('#'+dlgID+'_mess td').html(msg);
    aldlg.dialog('open');
    $('#'+dlgID+'_icon').show();
    $('#'+dlgID+'_mess').show();
}


app_class.prototype.GetTimePeriod = function( box, mess, cb ) {
	var caller = arguments.callee.caller;
	var bx = $('#'+box);
	var sdt = box + '_sdt';
	var fdt = box + '_fdt';
	var err = box + '_err';
	
	if( !bx.length ) {
		$('body').append('<div id="'+box+'" style="margin:8px 16px; width:300px;"></div>');
		bx = $('#'+box);
	}
	else
		if( bx.html() ) { bx.dialog('moveToTop'); return; }
    
	bx.html(
		'<div id="'+err+'" style="padding: 4px 8px; display:none" class="ui-widget ui-corner-all ui-widget-content ui-state-error"> \
			Дата начала перида должна быть указана обязательно</div> \
		<div style="margin:4px 0;">'+mess+'</div> \
		<table> \
			<tr><td style="text-align:right"><b>Начиная с даты</b> (включ.): </td><td><input type="text" style="height:20px;width:100px;" \
				id="'+sdt+'" name="'+sdt+'"></td></tr> \
			<tr><td style="text-align:right"><b>И заканчивая</b> (исключ.): </td><td><input type="text" style="height:20px;width:100px;" \
				id="'+fdt+'" name="'+fdt+'"></td></tr> \
		</table>'
	);
		
    $('#'+sdt).datepicker({
        dateFormat: "dd.mm.yy",
        showOn: "button",
        buttonImage: "img/calendar2.png",
        buttonImageOnly: true,
        buttonText: "Календарь",
        changeMonth: true,
        changeYear: true
    });

    $('#'+fdt).datepicker({
        dateFormat: "dd.mm.yy",
        showOn: "button",
        buttonImage: "img/calendar2.png",
        buttonImageOnly: true,
        buttonText: "Календарь",
        changeMonth: true,
        changeYear: true
    });

    bx.find(".ui-datepicker-trigger")
        .css({ 'cursor': 'pointer', 'position': 'relative', 'left': '-18px', 'top': '3px' });
    $('#'+sdt+', #'+fdt).attr("title", "Формат ввода: 'дд.мм.гггг'");
	
	bx.dialog({
		title: 'Требуется указание интервала времени',
		modal: true,
		resizable: false,
		height: 'auto',
		width: 'auto',
		stack: true,
		buttons: [ 
			{ text: "Принять", icons:{primary: "ui-icon-check"}, click: function() {
				var sdate = $('#'+sdt).val();
				var fdate = $('#'+fdt).val();
				if( !sdate ) {
					$( '#'+err).css({'display':'block'});
					return;
				}
				cb.call(caller,sdate,fdate);
				$( this ).dialog( "close" );
			} },
			{ text: "Отмена", icons:{primary: "ui-icon-close"}, click: function() { $( this ).dialog( "close" ); } }
		],
		open: function () { $('div[aria-describedby="'+box+'"]').shadow(); },
		close: function() {
			bx.dialog("destroy");
			$('#'+sdt).datepicker( "destroy" ).unbind();
			$('#'+fdt).datepicker( "destroy" ).unbind();
            bx.remove();
        }
	});
}


app_class.prototype.help = function( dlgID, title, app ) {
var aldlg = $('#'+dlgID);
var newdlg = $('#'+dlgID+'_HelpContent').length ? false : true;

    if( newdlg ) {
        aldlg.css({'overflow':'hidden', 'padding':'0px', 'padding-bottom':'14px' });
        aldlg.html( '<iframe id="'+dlgID+'_HelpContent" src=""'+
                    ' style="width:100%; height:100%; border: 1px solid #aaaaaa"></iframe>' );
        aldlg.dialog( {
            modal: false, title: title,
            zIndex: app.ui_dialog_min_z_index,
            height: 500, width: 680,
            position: ['right','bottom'],
            autoOpen: false, resizable: true,
            open: function () { 
				$('div[aria-describedby="' + dlgID + '"]').shadow(); 
			},
            close: function () {
                $(this).dialog("destroy");
                $.ui.dialog.maxZ = $.maxZIndex({ group: '.ui-dialog.ui-widget' });
            }
        } );
    }

    aldlg.dialog('open');
    
    ts = app.apptasks;
    HelpUrl = app.defhelpurl;
    for(i=0; i<ts.length; i++ ) if( ts[i].active && ts[i].helpurl ) { HelpUrl = ts[i].helpurl; break; }
    
    $('#'+dlgID+'_HelpContent').attr( 'src', HelpUrl );
}


app_class.prototype.ShowImage = function( img_id, photo_src, title) {
    if( photo_src == '' || img_id == '' ) return; 
    
    if($('#'+img_id).html()) {
        $('#'+img_id).dialog('moveToTop');
        return;
    }
    
    $('body').append('<div id="'+img_id+'" style="display:none"><img src="'+photo_src+'"></div>');

    var im = $('#'+img_id+' img');
    im.bind( 'load', function() {
        $('#'+img_id).dialog({
            title: title,
            modal: false,
            resizable: false,
            height: 'auto',
            width: 'auto',
//			show: { effect:'slide', speed:5000},
            stack: true,
			open: function () { $('div[aria-describedby="' + img_id + '"]').shadow() },
            close: function() {
                    $('#'+img_id).dialog("destroy");
                    $('#'+img_id).remove();
                 }
        });
        
        $('#'+img_id).parent().resizable( {'aspectRatio':true, 'alsoResize':im } );
    });
}                    

app_class.prototype.lib = {
  strip_tags: function(input, allowed) {
			allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
			var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
			commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
			return 	input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
						return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
					});
		}
}

app = new app_class();
